from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, Text, DateTime
from sqlalchemy.orm import relation, backref

Base = declarative_base()


#==== ==== ==== ====
# SOURCE_TYPE
#==== ==== ==== ====
class SourceType(Base):
    __tablename__  = "source_type"

    source_type_id = Column(Integer, primary_key=True)
    name = Column(String(128))
    handler = Column(String(64))

    def __repr__(self):
        return "<Source('%s','%s')>" % (self.category_id, self.name)


#==== ==== ==== ====
# SOURCE
#==== ==== ==== ====
class Source(Base):
    __tablename__  = "source"

    source_id = Column(Integer, primary_key=True)
    name = Column(String(128))
    status = Column(Integer)

    def __repr__(self):
        return "<Source('%s','%s')>" % (self.category_id, self.name)


#==== ==== ==== ====
# LOG
#==== ==== ==== ====
class Log(Base):
    __tablename__  = "log"

    process = Column(String(256))
    arguments = Column(Text)

    status = Column(Integer)
    value = Column(Text)

    # ETL
    etl_date = Column(DateTime, default=datetime.now)
    etl_id = Column(String(32))

    def __repr__(self):
        return "<Source('%s','%s')>" % (self.category_id, self.name)

#==== ==== ==== ====
# CATEGORY
#==== ==== ==== ====
class Category(Base):
    __tablename__  = "category"

    category_id = Column(Integer, primary_key=True)
    name = Column(String(128))

    def __repr__(self):
        return "<Category('%s','%s')>" % (self.category_id, self.name)


#==== ==== ==== ====
# LANGUAGE
#==== ==== ==== ====
class Language(Base):
    __tablename__ = "language"

    language_id = Column(Integer, primary_key=True)
    name = Column(String(32))

#==== ==== ==== ====
# TAG
#==== ==== ==== ====
class Tag(Base):
    __tablename__  = "tag"

    tag_id = Column(Integer, primary_key=True)
    name = Column(String(64))

    def __repr__(self):
        return "<Tag('%s','%s')>" % (self.name)

#==== ==== ==== ====
# NEWS_TAGS
#==== ==== ==== ====
news_tags = Table('news_tags', Base.metadata,
    Column('news_id', Integer, ForeignKey('news.news_id')),
    Column('tag_id', Integer, ForeignKey('tag.tag_id'))
    )

#==== ==== ==== ====
# GRAM
#==== ==== ==== ====
class Gram(Base):
    __tablename__  = "gram"

    gram_id = Column(Integer, primary_key=True)
    gram = Column(String(96))

    def __repr__(self):
        return "<Gram('%s')>" % (self.gram)

#==== ==== ==== ====
# NEWS_GRAMS
#==== ==== ==== ====
news_grams = Table('news_grams', Base.metadata,
    Column('news_id', Integer, ForeignKey('news.news_id')),
    Column('gram_id', Integer, ForeignKey('gram.gram_id')),
    Column('count', Float(16))
    )

#==== ==== ==== ====
# WORD
#==== ==== ==== ====
class Word(Base):
    __tablename__  = "word"

    word_id = Column(Integer, primary_key=True)
    word = Column(String(96))

    def __repr__(self):
        return "<Word('%s')>" % (self.word)

#==== ==== ==== ====
# NEWS_WORDS
#==== ==== ==== ====
news_words = Table('news_words', Base.metadata,
    Column('news_id', Integer, ForeignKey('news.news_id')),
    Column('word_id', Integer, ForeignKey('word.word_id')),
    Column('count', Integer)
    )


#==== ==== ==== ====
# NEWS
#==== ==== ==== ====
class News(Base):
    __tablename__  = "news"

    news_id = Column(Integer, primary_key=True)
    title = Column(String(256))
    link = Column(String(256))

    # Text...
    excerpt = Column(String(1000))
    html = Column(Text)
    relevantHtml = Column(Text)
    prettyHtml = Column(Text)
    text = Column(Text)
    normalizedText = Column(Text)

    # relations
    category = relation(Category)
    language = relation(Language)
    tags = relation('Tag', secondary=news_tags, backref='news')
    words = relation('Word', secondary=news_words, backref='news')
    grams = relation('Gram', secondary=news_grams, backref='news')
    similarity = relation(Similarity)

    # ETL
    status = Column(Text)
    etl_date = Column(DateTime, default=datetime.now)
    etl_id = Column(String(32))

    def __init__(self, link, title, html, excerpt='' , rank=-1, language_id=None, etl_id=''):
        self.link = link
        self.title = title
        self.excerpt = excerpt
        self.html = html
        self.category_id = category_id
        self.language = language_id
        self.etl_id = etl_id

    def __repr__(self):
        return "<News('%s','%s', '%s')>" % (self.title, self.link, self.text[:100])


#==== ==== ==== ====
# KERNEL
#==== ==== ==== ====
class Kernel(Base):
    __tablename__  = "kernel"

    kernel_id = Column(Integer, primary_key=True)
    kernel = Column(String(96))
    arg1 = Column(Float(32))
    arg2 = Column(Float(32))
    arg3 = Column(Float(32))
    arg4 = Column(Float(32))
    arg5 = Column(Float(32))

    def __repr__(self):
        return "<Kernel('%s')>" % (self.kernel)

#==== ==== ==== ====
# KERNEL_RESULTS
#==== ==== ==== ====
class Similarity(Base):
    __tablename__  = "similarity"

    news_id_1 = relation(News)
    news_id_2 = Column(Integer)
    similarity = Column(Float(32))
    kernel = relation(Kernel)

    def __init__(self, news_id_1, news_id_2, similarity, kernel_id):
        self.news_id_1 = news_id_1
        self.news_id_2 = news_id_2
        self.similarity = similarity
        self.kernel = kernel_id

    def __repr__(self):
        return u"<Similarity('%s', '%s', '%s', '%s')>" % (self.news_id_1, self.news_id_2, self.similarity, self.kernel)
